package com.jdcjk.project.car.view;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.SupportMapFragment;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.jdcjk.R;
import com.jdcjk.base.view.BaseFragment;
import com.jdcjk.project.car.presenter.CarPresenter;

import java.util.ArrayList;

/**
 * Created by dong on 2016/12/1.
 * 轨迹回放
 */
@Deprecated
public class TrackPlaybackFragment extends BaseFragment<CarPresenter> implements AMap.InfoWindowAdapter {

    private CarPresenter carPresenter;
    private AMap aMap;

    // 存放所有坐标的数组
    private ArrayList<LatLng> latlngList = new ArrayList<LatLng>();
    private ArrayList<LatLng> latlngList_path = new ArrayList<LatLng>();
    //模拟轨迹
    private static final LatLng marker1 = new LatLng(39.24426, 100.18322);
    private static final LatLng marker2 = new LatLng(39.24426, 104.18322);
    private static final LatLng marker3 = new LatLng(39.24426, 108.18322);
    private static final LatLng marker4 = new LatLng(39.24426, 112.18322);
    private static final LatLng marker5 = new LatLng(39.24426, 116.18322);
    private static final LatLng marker6 = new LatLng(36.24426, 100.18322);
    private static final LatLng marker7 = new LatLng(36.24426, 104.18322);
    private static final LatLng marker8 = new LatLng(36.24426, 108.18322);
    private static final LatLng marker9 = new LatLng(36.24426, 112.18322);
    private static final LatLng marker10 = new LatLng(36.24426, 116.18322);

    private boolean isTouch = false;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_track_playback;
    }

    @Override
    public CarPresenter bindPresenter() {
        carPresenter = new CarPresenter(getContext());
        return carPresenter;
    }

    @Override
    public void initContentView(View viewContent) {
        //back按钮添加监听
        viewContent.findViewById(R.id.back_btn).setOnClickListener(this);
        initMap();
    }

    @Override
    public void initData() {
// 加入坐标
        latlngList.add(marker1);
        latlngList.add(marker2);
        latlngList.add(marker3);
        latlngList.add(marker4);
        latlngList.add(marker5);
        latlngList.add(marker6);
        latlngList.add(marker7);
        latlngList.add(marker8);
        latlngList.add(marker9);
        latlngList.add(marker10);
        // latlngList_path.add(marker1);
        // 设置进度条最大长度为数组长度
//        processbar.setMax(latlngList.size());
//        aMap.setMapType(AMap.MAP_TYPE_NORMAL);
//        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlngList.get(0), 4));
    }

    //初始化地图
    private void initMap() {
        //初始化地图变量
        if (aMap == null) {
            aMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_car_track_play)).getMap();
        }
        // 设置自定义窗口
        aMap.setInfoWindowAdapter(this);
        // 设置自定义窗口
        aMap.setInfoWindowAdapter(this);
        // 获取Ui设定
        UiSettings mUiSettings = aMap.getUiSettings();
        // 设置显示缩放控件
        mUiSettings.setScaleControlsEnabled(true);
        // 设置所有手势可用
        mUiSettings.setAllGesturesEnabled(true);

    }

    //初始化车辆位置
    private void initCar() {
        Bundle bundle = getArguments();//从activity传过来的Bundle
        String carLicensePlate = bundle.getString("carLicensePlate");
        String carOnlineStatus = bundle.getString("carOnlineStatus");
        LatLng FirstMarkerLatLng = new LatLng(23.15598, 113.4520222);
        // 移动到该位置
        animateToLocation(FirstMarkerLatLng);
        addCarMarker(FirstMarkerLatLng, carOnlineStatus.equals("在线") ? true : false);
    }

    public void animateToLocation(LatLng latLng) {
        aMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
                new LatLng(latLng.latitude, latLng.longitude),//新的中心点坐标
                16, //新的缩放级别
                0, //俯仰角0°~45°（垂直与地图时为0）
                0  ////偏航角 0~360° (正北方为0)
        )));
    }

    private Marker addCarMarker(LatLng latLng, boolean isOnline) {
        // 创建标注物
        MarkerOptions markerOption = new MarkerOptions();
        // 设置地点位置（经纬度）
        markerOption.position(latLng);
        // 设置标注物的标题和描述--坑爹，必须设置
        markerOption.title("添加标注物").snippet("自定义标注物Window");
        // 设置标注物是否可以拖拽（长按拖拽）
        markerOption.draggable(true);
        // 设置标注物的自定义图标
        if (isOnline) {
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fragment_car_monitor_car_online)));
        } else {
            markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.fragment_car_monitor_car_offline)));
        }
        // 添加标注物至地图显示
        final Marker marker = aMap.addMarker(markerOption);
        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (!isTouch) {
                    if (marker.isInfoWindowShown()) {
                        marker.hideInfoWindow();
                    } else {
                        marker.showInfoWindow();
                    }
                }
                return true;
            }
        });
        aMap.setOnMapTouchListener(new AMap.OnMapTouchListener() {
            @Override
            public void onTouch(MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (marker.isInfoWindowShown()) {
                            marker.hideInfoWindow();
                            isTouch = true;
                        } else {
                            isTouch = false;
                        }
                        break;
                }
            }
        });
        marker.showInfoWindow();
        return marker;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        View infoWindow = getActivity().getLayoutInflater().inflate(R.layout.dialog_info_window_gaode, null);
        TextView name_textView = (TextView) infoWindow.findViewById(R.id.dialog_info_window_name_tv);
        TextView accStatus_textView = (TextView) infoWindow.findViewById(R.id.dialog_info_window_acc_status_tv);
        TextView onlineStatus_textView = (TextView) infoWindow.findViewById(R.id.dialog_info_window_online_status);
        name_textView.setText("ET210_44744");
        accStatus_textView.setText("关");
        onlineStatus_textView.setText("离线");
        return infoWindow;
    }

    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }
}
